package com.songcha.module_bookreader.ui.view

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.widget.FrameLayout

class BookReaderChapterAdvertContainerView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes) {
    //private var mLastDownX = 0f
    //private var mLastDeltaX = 0f

    interface IOnDispatchTouchEventListener{
        fun onActionDown(x:Float,y:Float)
        fun onActionMove(x:Float,y:Float)
        fun onActionUp()
        fun onActionCancel()
    }

    private var mScaledTouchSlop = 0
    private var mOnDispatchTouchEventListener:IOnDispatchTouchEventListener? = null
    private var mIsMoveSelf = false

    init {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        isClickable = true

        mScaledTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
    }

    fun setOnDispatchTouchEventListener(listener:IOnDispatchTouchEventListener){
        mOnDispatchTouchEventListener = listener
    }

    private fun getMotionEvent(action:Int):String{
        return when(action){
            MotionEvent.ACTION_DOWN->"ACTION_DOWN"
            MotionEvent.ACTION_MOVE->"ACTION_MOVE"
            MotionEvent.ACTION_UP->"ACTION_UP"
            MotionEvent.ACTION_CANCEL->"ACTION_CANCEL"
            else -> "unknow"
        }
    }

    private fun moveMyself(leftMargin:Float){
        val lp = layoutParams as LayoutParams
        lp.leftMargin = leftMargin.toInt()
        layoutParams = lp
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        //LogUtil.log(getMotionEvent(ev.action),tag = "aaa dispatchTouchEvent")
        when(ev.action){
            MotionEvent.ACTION_DOWN->{
                mOnDispatchTouchEventListener?.onActionDown(ev.x,ev.y)
            }
            MotionEvent.ACTION_MOVE->{
                mOnDispatchTouchEventListener?.onActionMove(ev.rawX,ev.y)
            }
            MotionEvent.ACTION_UP->{
                mOnDispatchTouchEventListener?.onActionUp()
            }
            MotionEvent.ACTION_CANCEL->{
                mOnDispatchTouchEventListener?.onActionCancel()
            }
        }

        return super.dispatchTouchEvent(ev)
    }

    /*private fun clearTouch(){
        mLastDownX = 0f
        mLastDeltaX = 0f
        mIsMoveSelf = false
    }*/

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        //if(childCount == 0) return super.onInterceptTouchEvent(ev)
        //return true
       // LogUtil.log(getMotionEvent(ev.action),tag = "aaa onInterceptTouchEvent")
        /*when(ev.action){
            MotionEvent.ACTION_DOWN->{
                //mLastDownX = ev.x
                //return false
                LogUtil.log("aaa1",ev.x)
            }
            MotionEvent.ACTION_MOVE->{
                LogUtil.log("aaa2",ev.x)
            }
            MotionEvent.ACTION_UP->{
               /* val deltaX = abs(ev.x - mLastDownX)
                if(deltaX < mScaledTouchSlop){
                    //click
                    //LogUtil.log("aaa 1")
                }else{
                    //slip
                    //LogUtil.log("aaa 2")
                }*/

            }
            MotionEvent.ACTION_CANCEL->{
                mLastDownX = 0f
            }
        }*/
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        //LogUtil.log(getMotionEvent(ev.action),tag = "aaa onTouchEvent")

        //滑动return false,点击return true
        /*when(ev.action){
            MotionEvent.ACTION_DOWN->{
                mLastDownX = ev.x
                return false
                //return super.onTouchEvent(ev)
            }
            MotionEvent.ACTION_MOVE->{
                val deltaX = abs(ev.x - mLastDownX)
                if(deltaX < mScaledTouchSlop){
                    //点击
                    return true
                }else{
                    //滑动
                    return false
                }
            }
            MotionEvent.ACTION_UP->{


            }
            MotionEvent.ACTION_CANCEL->{
                mLastDownX = 0f
            }
        }*/
        //return super.onTouchEvent(ev)
        //return false
        return super.onTouchEvent(ev)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
    }

}